package org.jsea.meta.api.service;

import lombok.Getter;
import lombok.Setter;
import org.jsea.meta.api.bean.ApiTableInfo;
import org.jsea.meta.api.bean.IMetaApiHandler;
import org.jsea.meta.api.bean.MetaApiInterceptor;
import org.jsea.meta.api.constant.ActionType;
import org.jsea.meta.api.service.dialect.IMetaApiDialect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * --.
 * *
 * author   Jiang.hy
 * create   2022-04-13
 */
@Getter
@Setter
@Component(value = "MetaApiProperties")
@ConditionalOnBean(value = MetaApiApplicationContext.class)
@ConfigurationProperties(prefix = "meta-api")
public class MetaApiProperties {

    private final Query query = new Query();
    private String contextPath;
    private Class<? extends MetaApiInterceptor> apply;
    private Class<? extends IMetaApiDialect> dbDialect;
    private Class<? extends IMetaApiHandler> result;// 结果处理
    private Map<String, ApiTableInfo> route = new HashMap<>();
    private String primaryKey = "id";
    private Boolean verifyAction = true;

    public void setRoute(Map<String, ApiTableInfo> route) {
        List<Class<? extends MetaApiInterceptor>> temp = null;
        for (Map.Entry<String, ApiTableInfo> entry : route.entrySet()) {
            ApiTableInfo value = entry.getValue();
            if (value.getSource() == null || value.getSource().isEmpty()) {
                value.setSource(entry.getKey());
            }
            if (value.getAction().isEmpty()) {

                /* [ jiang.hy 2024-02-20, 17:25, 周二 ]
                 . 不指定动作，默认仅拥有查询权限
                 增删改的动作要慎重授予
                 resource如果是组合结果，也不支持增删改
                 -                                  */
                value.getAction().add(ActionType.select);
            }
            if (value.getPrimaryKey() == null) {
                value.setPrimaryKey(primaryKey);
            }
            if (apply != null && value.getApply() == null) {
                if (temp == null) {
                    temp = new ArrayList<>(1);
                    temp.add(apply);
                }
                value.setApply(temp);
            }
        }
        this.route = route;
    }

    @Getter
    public static class Query {
        private int limitMax = 500;
        private int limitDef = 20;

        public void setLimitMax(int limitMax) {
            if (limitMax <= 0) {
                throw new IllegalArgumentException("[meta-api.query.limit-max: " + limitMax + " ] must be greater than 0");
            }
            this.limitMax = limitMax;
        }

        public void setLimitDef(int limitDef) {
            if (limitDef <= 0) {
                throw new IllegalArgumentException("[meta-api.query.limit-def: " + limitDef + " ] must be greater than 0");
            }
            this.limitDef = limitDef;
        }
    }
}
